﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GeneratorLib.DB;
using GeneratorLib.DB.Connectios;
using GeneratorLib.DB.Schemas;

namespace Generator
{
    public partial class FrmDBRelationEdit : Form
    {
        public FrmDBRelationEdit()
        {
            InitializeComponent();
        }


        public Relation RelationShip
        {
            get
            {
                Relation relation = new Relation();
                RelationTable tab1 = new RelationTable();
                RelationTable tab2 = new RelationTable();

                tab1.TableName = PrimaryTable.SelectedItem.ToString();
                tab1.KeyFieldName = PrimaryField .SelectedItem.ToString();
                tab1.ValueFieldName = DisplayField.SelectedItem.ToString();

                tab2.TableName = ForeignTable .SelectedItem.ToString();
                tab2.KeyFieldName = ForeignField.SelectedItem.ToString();
                relation.PrimaryTable = tab1;
                relation.ForeignTable = tab2;
                return relation;
            }
        }

        private IConnection _connection;
        private TableInfo _table;
        private FieldInfo _field;
        public IConnection Connection
        {
            get
            {
                return _connection;
            }
            set
            {
                _connection = value;
                FillTable();
            }
        }

        public TableInfo Table
        {
            get
            {
                return _table;
            }
            set {
                _table = value;
                if (_table != null)
                    this.Connection = _table.Connection;
            }
        }

        public FieldInfo Field
        {
            get
            {
                return _field;
            }
            set
            {
                _field = value;
                if (_field != null)
                    this.Table = _field.Table;
            }
        }

        private void FillTable()
        {
            PrimaryTable.Items.Clear();
            ForeignTable.Items.Clear();
            PrimaryField.Items.Clear();
            ForeignField.Items.Clear();
            DisplayField.Items.Clear();

            List<TableInfo> tables = Connection.Tables;
            foreach (var table in tables)
            {
                PrimaryTable.Items.Add(table);
                ForeignTable.Items.Add(table);
            }            
        }

        private void FrmDBRelationEdit_Load(object sender, EventArgs e)
        {
            if (Table != null)
            {
                if (Table.IsMasterTable)
                {
                    PrimaryTable.SelectedItem = Table;
                }
                else
                {
                    ForeignTable.SelectedItem = Table;
                }
            }
        }

        private void PrimaryTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            TableInfo table = (TableInfo)PrimaryTable.SelectedItem;
            PrimaryField.Items.Clear();
            DisplayField.Items.Clear();
            DisplayField.Items.Add("");

            foreach (FieldInfo field in table.Fields)
            {
                PrimaryField.Items.Add(field);
                DisplayField.Items.Add(field);
            }

            FieldInfo keyField= table.GetKeyField();
            if (keyField != null)
            {
                PrimaryField.SelectedItem = keyField;
            }

            FieldInfo disField = table.GetDisplayValueField();
            if (disField != null)
            {
                DisplayField.SelectedItem = disField;
            }
            else
            {
                DisplayField.SelectedIndex = 0;
            }
        }

        private void ForeignTable_SelectedIndexChanged(object sender, EventArgs e)
        {
            TableInfo table = (TableInfo)ForeignTable.SelectedItem;
            ForeignField.Items.Clear();

            foreach (FieldInfo field in table.Fields)
            {
                ForeignField.Items.Add(field);
            }

            
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (PrimaryTable.SelectedIndex < 0)
            {
                PrimaryTable.Focus();
                return;
            }

            if (PrimaryField.SelectedIndex < 0)
            {
                PrimaryField.Focus();
                return;
            }

            if (ForeignTable.SelectedIndex < 0)
            {
                ForeignTable.Focus();
                return;
            }

            if (ForeignField.SelectedIndex < 0)
            {
                ForeignField.Focus();
                return;
            }

            if (PrimaryTable.SelectedIndex == ForeignTable.SelectedIndex)
            {
                ForeignField.Focus();
                return;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
